package com.stenio.fancat.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.stenio.fancat.domain.bo.JoinActivityBO;
import com.stenio.fancat.domain.bo.PublishActivityBO;
import com.stenio.fancat.domain.bo.UserActivityBO;
import com.stenio.fancat.domain.bo.UserBO;
import com.stenio.fancat.form.UserForm;
import com.stenio.fancat.service.UserService;
import com.stenio.fancat.util.ResultUtil;
import com.stenio.fancat.vo.Result;

/**
 * 用户模块controller
 */
@RestController
@RequestMapping("user")
public class UserController {

	@Autowired
	private UserService userService;

	/**
	 * 获取用户、账户信息
	 * 
	 * @param userForm
	 *            用户form实体类
	 * @return
	 */
	@PostMapping("center")
	@ResponseBody
	public Result queryUserInfo(@RequestBody UserForm userForm) {
		UserBO userBO = null;
		try {
			userBO = userService.queryUserInfoByCondition(userForm);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error("1", e.getMessage());
		}
		return ResultUtil.success(userBO);
	}

	/**
	 * 活动初始化发布接口，获取用户相关的品牌、账户
	 * 
	 * @param id
	 *            用户Id
	 * @return
	 */
	@PostMapping("publishInit")
	@ResponseBody
	public Result queryBrand(@RequestBody UserForm userForm) {
		UserActivityBO uaBO = null;
		try {
			if (null == userForm)
				return ResultUtil.error("1", "参数不能为空");
			uaBO = userService.queryUserInfoByUserId(userForm.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error("1", e.getMessage());
		}
		return ResultUtil.success(uaBO);
	}

	/**
	 * 获取用户完成的活动
	 * 
	 * @param id
	 *            用户Id
	 * @return
	 */
	@PostMapping("doneActivitiy")
	@ResponseBody
	public Result queryUserJoinAct(@RequestBody UserForm userForm) {
		Page<JoinActivityBO> page;
		try {
			page = userService.queryUserJoinAct(userForm);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error("1", e.getMessage());
		}
		return ResultUtil.success(page);
	}

	/**
	 * 获取用户发布的活动
	 * 
	 * @param id
	 *            用户Id
	 * @return
	 */
	@PostMapping("publishActivitiy")
	@ResponseBody
	public Result queryUserPublishAct(@RequestBody UserForm userForm) {
		Page<PublishActivityBO> page;
		try {
			page = userService.queryUserPublishAct(userForm);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error("1", e.getMessage());
		}
		return ResultUtil.success(page);
	}

	/**
	 * 新增更新用户，包含用户信息、账户信息、图片信息
	 * 
	 * @param userBO
	 * 
	 * @return
	 */
	@PostMapping("update")
	@ResponseBody
	public Result saveUser(@RequestBody UserForm userForm) {
		try {
			userService.saveUser(userForm);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error("1", e.getMessage());
		}
		return ResultUtil.success();
	}

}
